import 'package:date_format/date_format.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:imember/Util/MyColor.dart';
import 'package:imember/Widget/MyButton.dart';
import 'package:imember/Widget/MyDialog.dart';
import 'package:imember/Widget/MyImage.dart';
import 'package:imember/Widget/MyInput.dart';
import 'package:imember/Widget/MyPage.dart';
import 'package:keyboard_utils/keyboard_listener.dart';
import 'package:keyboard_utils/keyboard_utils.dart';

import 'OrderDetail.dart';

class OrderRecord extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _OrderRecord();
  }
}

class _OrderRecord extends State {
  String startDate = "";
  String endDate = "";

  KeyboardUtils _keyboardUtils = KeyboardUtils();

  @override
  void initState() {
    _keyboardUtils.add(
        listener: KeyboardListener(
            willHideKeyboard: () {
              FocusManager.instance.primaryFocus?.unfocus();
            },
            willShowKeyboard: (double keyboardHeight) {}));
    super.initState();
  }

  @override
  void dispose() {
    if (_keyboardUtils.canCallDispose()) {
      _keyboardUtils.dispose();
    }
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return MyPage(
        Container(
          child: Column(
            children: [
              Row(mainAxisAlignment: MainAxisAlignment.center, children: [
                Container(width: 30),
                Text("日期範圍", style: TextStyle(fontSize: 17, color: MyColor.C70)),
                Container(width: 19),
                CalendarChooser(startDate, onConfirm: (DateTime dateTime, List<int> selectedIndex) {
                  setState(() {
                    startDate = formatDate(dateTime, [yyyy, '-', mm, '-', dd]);
                  });
                }),
                Container(height: 3, width: 12, color: MyColor.C70, margin: EdgeInsets.all(5)),
                CalendarChooser(endDate, onConfirm: (DateTime dateTime, List<int> selectedIndex) {
                  setState(() {
                    endDate = formatDate(dateTime, [yyyy, '-', mm, '-', dd]);
                  });
                }),
                Expanded(child: Container()),
                Text("關鍵字搜尋", style: TextStyle(fontSize: 17, color: MyColor.C70)),
                Container(
                  width: 189,
                  height: 36,
                  padding: EdgeInsets.fromLTRB(12, 0, 12, 0),
                  margin: EdgeInsets.only(left: 8),
                  decoration: BoxDecoration(
                      borderRadius: BorderRadius.all(Radius.circular(24)), border: Border.all(width: 1.2, color: MyColor.C58)),
                  child: NormalInput(),
                ),
                LocalImage("ic_search.png", width: 30, padding: EdgeInsets.all(13)),
                Container(width: 30),
              ]),
              Container(height: 2, color: MyColor.DD),
              Expanded(
                child: true
                    ? Container(
                        child: ListView.separated(
                            padding: EdgeInsets.all(0),
                            itemCount: 4,
                            itemBuilder: (BuildContext context, int index) {
                              return ItemOrderRecord();
                            },
                            separatorBuilder: (BuildContext context, int index) {
                              return Container(height: 2, color: MyColor.DD);
                            }))
                    : Center(child: Text("目前尚無紀錄", style: TextStyle(fontSize: 30, color: MyColor.C70, fontWeight: FontWeight.bold))),
              )
            ],
          ),
        ),
        showNotice: true,
        showIcon: true);
  }
}

class ItemOrderRecord extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.fromLTRB(31, 12, 34, 12),
      child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
        Text("\$53", style: TextStyle(fontSize: 20, color: MyColor.C51)),
        Container(height: 12),
        Row(children: [
          Text("訂單編號:", style: TextStyle(fontSize: 20, color: MyColor.C51)),
          Container(width: 120, child: Text("113834", style: TextStyle(fontSize: 20, color: MyColor.C99))),
          Expanded(child: Text("狀態: 已完成", style: TextStyle(fontSize: 20, color: MyColor.C51))),
          Text("2020/12/22 15:56", style: TextStyle(fontSize: 20, color: MyColor.C51)),
          OrangeBtn("明細", height: 29, width: 62, fontSize: 15, margin: EdgeInsets.fromLTRB(17, 0, 0, 0), onTap: () {
            showWidgetDialog(context, OrderDetail());
          })
        ])
      ]),
    );
  }
}
